﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using log4net;
using YoProgramo.Domain.Core.Interfaces;
using YoProgramo.Infrastructure.Tools.Configuration;

namespace YoProgramo.Domain.BusinessLogic.Service
{
    public enum MailType
    {
        Welcome, SpamReport, Info
    }

    public class MailService : IMailService {
        private static readonly ILog log = LogManager.GetLogger(typeof(MailService));

        public MailService(IUserService u)
        {
            UserService = u;
        }


        public IUserService UserService { get; set; }
        #region IMailService Members

        public string SmtpHost { get; set; }


        public void SendMail(string from, string to, string subject, string body, bool isHtml, List<System.Net.Mail.Attachment> attachments) {
            SmtpClient client;
            MailMessage mail = new MailMessage();

            mail.To.Add(new MailAddress(to));
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isHtml;
            if (!string.IsNullOrEmpty(from)) mail.From = new MailAddress(from);

            if (attachments != null)
            {
                foreach (Attachment a in attachments) {
                    mail.Attachments.Add(a);
                }
            }

            try {
                if (string.IsNullOrEmpty(SmtpHost)){
                    client = new SmtpClient();
                }
                else{
                    client = new SmtpClient(SmtpHost);
                }
                    
                                        
                client.Send(mail);
            } catch (Exception MailEx) {
                throw new Exception(MailEx.Message);
            } 
        }


        public void SendMail(string from, string username, MailType t, string templateFolder) {
            if(MailType.Welcome == t)
            {
                StreamReader streamReader = new StreamReader(templateFolder + "Welcome.txt");
                string body = streamReader.ReadToEnd();
                streamReader.Close();

                IUser user = UserService.GetByUserName(username);
                if(user != null)
                {
                    body = body.Replace("[UserName]", username);
                    body = body.Replace("[SiteName]", YoProgramoConfigurationManager.GetSiteName());

                    /*Subject*/
                    string subject = null;
                    int subjectTagStart = body.IndexOf("[Subject]");
                    int subjectTagEnd = body.IndexOf("[/Subject]");
                    if(subjectTagStart > 0 && subjectTagEnd > 0)
                    {
                        subject = body.Substring(subjectTagStart + 9, subjectTagEnd - (subjectTagStart + 9));
                        body = body.Substring(0, subjectTagStart);
                    }                    

                    SendMail(from, user.Email, subject ?? "Bienvenidos a YoProgramo.NET", body, true, null);

                    log.Info("Mail sended | User: " + user.UserName + " | EMail: " + user.Email + " | MailType: " + t);
                    return;
                }
                log.Error("User not found | User: " + username + " | MailType: " + t);
            }
        }

        #endregion
    }
}